package com.clouddot.baipai.ui.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.PagerAdapter;
import android.text.Html;
import android.text.Spanned;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.webkit.WebView;
import android.widget.TextView;

import com.clouddot.baipai.R;
import com.clouddot.baipai.http.entity.response.getArcticByIdResponse;
import com.clouddot.baipai.infrastructure.environment.Env;
import com.clouddot.baipai.infrastructure.environment.environment;
import com.clouddot.baipai.util.EscapeUnescapeUtil;
import com.clouddot.baipai.util.ThreadPool.ThreadPool;


import java.io.InputStream;
import java.net.URL;
import java.util.List;

/**
 * Created by wxy on 2018/5/10.
 */

public class UrgentAdapter extends PagerAdapter {


    private List<getArcticByIdResponse> mItems;
    private Context mContext;
    private LayoutInflater mInflater;

    public UrgentAdapter(List<getArcticByIdResponse> items, Context context) {
        mContext = context;
        mInflater = LayoutInflater.from(context);
        mItems = items;
    }

    /**
     * @param container
     * @param position
     * @return 对position进行求模操作
     * 因为当用户向左滑时position可能出现负值，所以必须进行处理
     */
    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        position %= mItems.size();
        getArcticByIdResponse response = mItems.get(position);

        final hander hander = new hander();
        hander.title.setText(response.BaseData.Title);
        hander.time.setText(response.BaseData.CreateTimeText);
        final String html = EscapeUnescapeUtil.unescape(response.BaseData.Detail);
        hander.webView.setBackgroundColor(0);
        hander.webView.getSettings().setDefaultTextEncodingName("UTF -8");
        hander.webView.loadDataWithBaseURL(null, html, "text/html", "utf-8", null);
        hander.webView.setVisibility(View.VISIBLE);


        //如果View已经在之前添加到了一个父组件，则必须先remove，否则会抛出IllegalStateException。
        ViewParent viewParent = hander.ret.getParent();
        if (viewParent != null) {
            ViewGroup parent = (ViewGroup) viewParent;
            parent.removeView(hander.ret);
        }
        container.addView(hander.ret);
        return hander.ret;
    }


    /**
     * 由于我们在instantiateItem()方法中已经处理了remove的逻辑，
     * 因此这里并不需要处理。实际上，实验表明这里如果加上了remove的调用，
     * 则会出现ViewPager的内容为空的情况。
     *
     * @param container
     * @param position
     * @param object
     */
    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        //警告:不要在这里调用removeView, 已经在instantiateItem中处理了
    }


    @Override
    public int getCount() {
        int ret = 0;
        if (mItems.size() > 0) {
            ret = mItems.size();
        }
        return ret;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == (View) object;
    }


    private class hander {
        private View ret = null;
        TextView title;
        TextView time;
        WebView webView;

        public hander() {
            ret = LayoutInflater.from(mContext).inflate(R.layout.h_adapter_urgentcontent, null);

            title = ret.findViewById(R.id.title);
            time = ret.findViewById(R.id.time);
            webView = ret.findViewById(R.id.content);
        }
    }

}